<?php defined('SYSPATH') or die('No direct script access.');

class Report_Model extends Model {

	public function __construct() {		
		$this->db = Database::instance();
	}

	public function getReportData($reportType,$reportCondition,$condition){
		kohana::log('debug','Report : getReportData');
		$sql  = '';
		$tableName = '';
		$sqlCondition = '';

		if($reportCondition == 'day'){
			$sqlCondition = 'DAY(created) = ?';
		}else if($reportCondition == 'month'){
			$sqlCondition = 'MONTH(created) = 1';
		}else if($reportCondition == 'year'){
			$sqlCondition = 'YEAR(created) = ?';
		}

		if($reportType == 'download'){
			$tableName = 'profiles_download_songs';
		}else{
			$tableName = 'profiles_vote_songs';
		}
		
		$sql =  'select a.amount , a.track_no , b.title from'
			        .'(SELECT count(track_no) as amount, track_no , album_id '
					.'FROM '.$tableName.' WHERE '.$sqlCondition.' group by track_no , album_id '
					.' order by count(track_no) desc) a , songs b where a.track_no = b.track_no'
					.' and a.album_id = b.album_id'
					.' LIMIT 0,10';
		kohana::log('debug','Report : sql '.$sql.' :: condition '.(int)$condition);

		$query = $this->db->query($sql,array((int)$condition));

		kohana::log('debug','Report : finish query');
		kohana::log('debug','Report : finish getReportData');

		return $query->result(FALSE);
	}
}